package dbs;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * SQLServer 连接调用
 * 
 * 连接方式摘自 Microsoft 官方 http://msdn.microsoft.com/en-us/library/ms378428.aspx
 * 
 * @author Beanstao@gmail.com
 * @date 2013-10-15上午9:26:18
 * @version 1.0
 */
/**
 * Connect to the default database on the local computer by using a user name and password:
 * jdbc:sqlserver://localhost;user=MyUserName;password=*****;
 * 
 * Connect to the default database on the local computer by using integrated authentication:
 * jdbc:sqlserver://localhost;integratedSecurity=true;
 * 
 * Connect to a named database on a remote server:
 * jdbc:sqlserver://localhost;databaseName=AdventureWorks;integratedSecurity=true;
 * 
 * Connect on the default port to the remote server:
 * jdbc:sqlserver://localhost:1433;databaseName=AdventureWorks;integratedSecurity=true;
 * 
 * Connect by specifying a customized application name:
 * jdbc:sqlserver://localhost;databaseName=AdventureWorks;integratedSecurity=true;applicationName=MyApp;
 */
public class SQLServerConnection
{
	public static void main(String[] args)
	{
		DBCommon.displayDbProperties(_Connection);
	}

	private final static String url = "jdbc:sqlserver://";
	private final static String serverName = "192.168.2.47";// "192.168.2.21";
	private final static String portNumber = "1433";
	private final static String databaseName = "master";// "Pan_Queue_T";
	private final static String selectMethod = "cursor";

	// jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
	private static String getConnectionUrl()
	{
		return url + serverName + ":" + portNumber + ";databaseName=" + databaseName + ";selectMethod=" + selectMethod + ";";
	}

	private static String userName = "sa";
	private static String password = "sa111111";// "0123";
	private static Connection _Connection = null;

	static
	{
		try
		{
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
			_Connection = DriverManager.getConnection(getConnectionUrl(), userName, password);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			System.out.println("Error Trace in getConnection() : " + e.getMessage());
		}
		if (_Connection != null)
			System.out.println("连接成功");

	}

}
